package com.gpnu.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpnu.crm.domain.SystemDictionary;
import com.gpnu.crm.domain.SystemDictionaryItem;
import com.gpnu.crm.mapper.SystemDictionaryItemMapper;
import com.gpnu.crm.mapper.SystemDictionaryMapper;
import com.gpnu.crm.query.QueryObject;
import com.gpnu.crm.service.ISystemDictionaryService;


@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
	
	  @Autowired
	  private SystemDictionaryMapper dictionaryMapper;
	  @Autowired
	  private SystemDictionaryItemMapper itemMapper;
	  
	  public SystemDictionaryServiceImpl() {}
	  
	  public List<SystemDictionaryItem> queryItemByParentSn(String parentSn)
	  {
	    return itemMapper.selectByParentSn(parentSn);
	  }
	  
	  public void deleteItem(Long id) {
	    itemMapper.deleteByPrimaryKey(id);
	  }
	 
	  public void saveOrUpdateItem(SystemDictionaryItem entity)
	  {
	    if (entity.getSequence() == null) {
	      Integer maxSequence = itemMapper.selectMaxSequence(entity.getParentId());
	      if (maxSequence == null) {
	        maxSequence = Integer.valueOf(0);
	      }
	      entity.setSequence(Integer.valueOf(maxSequence.intValue() + 1));
	    }
	    
	    if (entity.getId() == null) {
	      itemMapper.insert(entity);
	    } else {
	      itemMapper.updateByPrimaryKey(entity);
	    }
	  }
	  
	  public PageInfo<SystemDictionaryItem> queryItem(QueryObject qo)
	  {
	    PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderby());
	    List<SystemDictionaryItem> list = itemMapper.selectForList(qo);
	    return new PageInfo(list);
	  }
	  
	  public PageInfo<SystemDictionary> query(QueryObject qo)
	  {
	    PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
	    List<SystemDictionary> list = dictionaryMapper.selectForList(qo);
	    return new PageInfo(list);
	  }
	  
	  public void saveOrUpdate(SystemDictionary entity) {
	    if (entity.getId() == null) {
	      dictionaryMapper.insert(entity);
	    } else {
	      dictionaryMapper.updateByPrimaryKey(entity);
	    }
	  }
	  
	  public void delete(Long id)
	  {
	    itemMapper.deleteByParentId(id);
	    dictionaryMapper.deleteByPrimaryKey(id);
	  }
	  
	  public List<SystemDictionary> list() {
	    return dictionaryMapper.selectAll();
	  }
}
